<?php
namespace app\massage\model;

use app\BaseModel;
use think\facade\Db;

class Goods extends BaseModel
{
    //定义表名
    protected $name = 'shequshop_car_goods';


    protected $append = [

        'create_time_text',

        'car_type'

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-09-22 10:53
     * @功能说明:
     */
    public function getCarTypeAttr($value,$data){

        if(!empty($data['id'])){

            $car_connect_type = new CarTypeConnect();

            $dis = [

                'order_goods_id' => 0,

                'goods_id' => $data['id']
            ];

            $list = $car_connect_type->where($dis)->column('type_id');

            return array_values($list);
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-10-12 14:17
     * @功能说明:判断该车型是否在使用
     */
    public function carTypeHave($car_type_id){

        $dis [] = ['a.status','>',-1];

        $dis [] = ['b.type_id','>',$car_type_id];

        $data = $this->alias('a')
                ->join('shequshop_car_cartype_connect b','a.id = b.goods_id')
                ->where($dis)
                ->find();

        return !empty($data)?$data->toArray():[];

    }





    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2021-03-23 11:12
     */
    public function getImgsAttr($value,$data){

        if(!empty($value)){

            return explode(',',$value);

        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-03-23 11:12
     * @功能说明:
     */
    public function getCreateTimeTextAttr($value,$data){

        if(!empty($data['create_time'])){

            return date('Y-m-d H:i:s',$data['create_time']);
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2021-09-22 10:46
     * @功能说明:添加车型
     */
    public function updateSome($car_type,$id,$uniacid){

        $car_connect_type = new CarTypeConnect();

        $dis = [

            'order_goods_id' => 0,

            'goods_id' => $id
        ];

        $car_connect_type->where($dis)->delete();

        if(!empty($car_type)){

            foreach ($car_type as $k=> $value){

                $insert[$k]['goods_id'] = $id;

                $insert[$k]['uniacid']  = $uniacid;

                $insert[$k]['type_id']  = $value;

            }

            $car_connect_type->saveAll($insert);

        }

        return true;

    }




    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $data['create_time'] = time();

        if(isset($data['car_type'])){

            $car_type = $data['car_type'];

            unset($data['car_type']);
        }

//        $data['imgs'] = !empty($data['imgs'])?implode(',',$data['imgs']):'';

        $res = $this->insert($data);

        if(!empty($car_type)){

            $id = $this->getLastInsID();

            $this->updateSome($car_type,$id,$data['uniacid']);
        }

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        if(isset($data['car_type'])){

            $car_type = $data['car_type'];

            unset($data['car_type']);
        }

        $res = $this->where($dis)->update($data);

        if(!empty($car_type)){

            $this->updateSome($car_type,$data['id'],$data['uniacid']);
        }

        return $res;

    }





//    /**
//     * @author chenniang
//     * @DataTime: 2020-09-29 11:05
//     * @功能说明:编辑
//     */
//    public function dataUpdate($dis,$data){
//
//        $res = $this->where($dis)->update($data);
//
//        return $res;
//
//    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function indexDataList($dis,$page,$sort){

        $data = $this->where($dis)->order("$sort,id desc")->paginate($page)->toArray();

        return $data;

    }









    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2021-07-07 10:21
     * @功能说明:服务技师列表
     */
    public function serviceCoachList($dis){

        $data = $this->alias('a')
            ->join('massage_service_service_coach b','a.id = b.ser_id')
            ->where($dis)
            ->field(['a.*'])
            ->order('a.id desc')
            ->select()
            ->toArray();

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2021-03-18 10:07
     * @功能说明:增加|减少库存 增加|减少销量
     */
    public function setOrDelStock($goods_id,$num,$type=2){

        if(empty($goods_id)){

            return true;
        }

        $goods_info = $this->dataInfo(['id'=>$goods_id]);
        //退货
        if($type==1){

            $update = [

                'true_sale' => $goods_info['true_sale']-$num,

                'total_sale'=> $goods_info['total_sale']-$num,

                'lock'      => $goods_info['lock']+1,

            ];
            //如果是售后增加退款数量
//            if($refund==1){
//
//                $update['refund_num'] = $goods_info['refund_num']+$num;
//            }
            //减销量 加退款数量
            $res = $this->where(['id'=>$goods_id,'lock'=>$goods_info['lock']])->update($update);

            if($res!=1){

                return ['code'=>500,'msg'=>'提交失败'];
            }

        }else{

            $update = [

                'true_sale' => $goods_info['true_sale']+$num,

                'total_sale'=> $goods_info['total_sale']+$num,

                'lock'      => $goods_info['lock']+1,

            ];

            //增加销量
            $res = $this->where(['id'=>$goods_id,'lock'=>$goods_info['lock']])->update($update);

            if($res!=1){

                return ['code'=>500,'msg'=>'提交失败'];
            }


        }

        return true;
    }






}